package cc.rengu.redp.bizimpl.igas.controller;


import cc.rengu.redp.bizimpl.upmp.vo.WaitingToDoVO;
import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.igas.domain.PBinInfo;
import cc.rengu.redp.bizimpl.igas.service.IPBinInfoService;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author System
 */
@Slf4j
@RestController
@RequestMapping("/param/pbininfo")
public class PBinInfoController extends BaseController {

    @Autowired
    private IPBinInfoService pBinInfoService;

    /**
     * 查询分页列表
     *
     * @param queryRequest
     * @return
     */
    @GetMapping
//    @RequiresPermissions("pbininfo:list")
    public Map<String, Object> getPageList(QueryRequest queryRequest, PBinInfo pBinInfo) throws RedpException {
//        if(StringUtils.isEmpty(pBinInfo.getInstId())){
//            pBinInfo.setInstId(McmpConstant.DEFAULT_INST_ID);
//        }
        return getDataTable(pBinInfoService.findByPage(queryRequest,pBinInfo));
    }
    /**
     * 新增 对账场景管理
     *
     * @param pBinInfo
     * @return
     */
    @Log("卡BIN信息新增")
    @PutMapping("/add")
//    @RequiresPermissions("pbininfo:add")
    public void add(PBinInfo pBinInfo)throws RedpException {
        pBinInfoService.add(pBinInfo);
    }

    /**
     * 修改 卡BIN信息
     *
     * @param pBinInfo
     * @return
     */
    @Log("卡BIN信息修改")
    @PutMapping("/update")
//    @RequiresPermissions("pbininfo:update")
    public void update(PBinInfo pBinInfo)throws RedpException {
        pBinInfoService.update(pBinInfo);
    }

    /**
     * 审核 卡BIN信息
     *
     * @param taskInfo
     * @return
     */
    @Log("卡BIN信息新增、修改审核")
    @PutMapping("/check")
    @RequiresPermissions("pbininfo:check")
    public void check(WaitingToDoVO taskInfo)throws RedpException {
        pBinInfoService.check(taskInfo);
    }

    /**
     * 详情 卡bin
     *
     * @param pBinInfo
     * @return
     */
    @GetMapping("/detail")
    @RequiresPermissions("pbininfo:detail")
    public void detail(@Valid PBinInfo pBinInfo) {

    }

    @PostMapping("/excel")
    @RequiresPermissions("pbininfo:export")
    @Log("导出卡bin信息")
    public void export(QueryRequest queryRequest, PBinInfo pBinInfo, HttpServletResponse response) throws RedpException {
        String message;
        try {
            List<PBinInfo> destList = pBinInfoService.findByPage(queryRequest,pBinInfo).getRecords();
            ExcelKit.$Export(PBinInfo.class, response).downXlsx(destList, false);
        } catch (Exception e) {
            message = "导出卡bin信息";
            log.error(message, e);
            throw new RedpException(message);
        }
    }


    /**
     * 查询发卡机构 --makeby lizk
     *
     * @param q
     * @param request
     * @return
     */
    @GetMapping("getCardBinSelect2List")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "q", value = "参数", required = true, dataType = "String")
    })
    public Map<String, Object> getTblTxnInfSelect2List(String q, QueryRequest request){
        return pBinInfoService.getTblTxnInfSelect2List(q, request);
    }

    /**
     * 新增发卡机构 -makeby lizk
     *
     * @param binInfo
     * @throws RedpException
     */
    @PostMapping("addBinInfo")
    public void addBinInfo(PBinInfo binInfo) throws RedpException{
        try {
            this.pBinInfoService.addBinInfo(binInfo);
        } catch (Exception e) {
            String message = "新增发卡机构失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    /**
     * 修改或注销发卡机构 -makeby lizk
     *
     * @param binInfo
     * @throws RedpException
     */
    @PutMapping("updateBinInfo")
    public void updateBinInfo(PBinInfo binInfo) throws RedpException{
        try {
            this.pBinInfoService.updateBinInfo(binInfo);
        } catch (Exception e) {
            String message = "修改或注销发卡机构失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

}
